package entities;

import interfaze.SpaceInvadersGamingPanel;

import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import logic.Utilities;

@SuppressWarnings("serial")
public class DefenderShot extends MovableEntity {

	ImageIcon icon;
	/**
	 * creates a defender shot class movable entity on the specified coordinates, which the specified speed on the specified panel
	 * @param x
	 * @param y coordinates
	 * @param dx
	 * @param dy speeed
	 * @param p panel to which the shot belongs
	 */
	public DefenderShot(int x, int y, int dx, int dy, SpaceInvadersGamingPanel p) {
		super(x, y, dx, dy);
		this.p=p;
		Image image = Utilities.loadImage("res/PLAYERSHOT.png");
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds((int)this.x, (int)this.y, this.width, this.height);
		lastUpdate=System.currentTimeMillis();
	}
	/**
	 * this method checks whether the bounds of an enemy and a shot intersect, if they do, both the shot and enemy are made invisible and there's a chace a power up will be dropped
	 * @param listaEnemigos stores all the enemies on screen
	 * @param lPu stores all power ups, necessary if a powerup is dropped
	 * @return
	 */
	public Enemy impacts(ArrayList<Enemy> listaEnemigos,ArrayList<PowerUp> lPu) {
		Enemy temp=null;
		if (isVisible()) {
				for (Enemy e : listaEnemigos) {
					if (e.isVisible()) {
					    Rectangle r1=this.getBounds();
					    Rectangle r2=e.getBounds();
						if(r1.intersects(r2)){
							temp=e;
							e.setVisible(false);
							this.setVisible(false);
							if ((int) ((4 - 1) * Math.random() + 1) == (int) ((4 - 1)
									* Math.random() + 1)) {
								int choice = (int) ((50 - 1) * Math.random() + 1);
								if (choice % 2==0) {
										PowerUp temp2= new ShotSpeedUp((int)e.x, (int)e.y, 0, 100,1,1,p);
										p.add(temp2, 0);
										lPu.add(temp2);
								} else {
										PowerUp temp2= new RelSpeedUp((int)e.x, (int)e.y,0, 100,1,1,p);
										p.add(temp2, 0);
										lPu.add(temp2);
								}
							}
						}
					}
				}
			}
		return temp;
	}
}
